﻿using AutoMapper;
using B.S.BaseData.Domain;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure.Interfaces;
using B.S.BaseData.ReadAPI.Applications.Commands.DangerousCommand;
using B.S.BaseData.ReadAPI.Dtos;
using MediatR;
using System.Collections.Generic;

namespace B.S.BaseData.ReadAPI.Applications.CommandHandlers.DangerousCommandHandler
{
    public class PagingCommandHandler : IRequestHandler<PagingCommand, ApiResult<ApiPaging<List<PagingDangerousRespDto>>>>
    {
        IBaseRepository<Dangerous> dangerousrepository;
        IBaseRepository<Car> carrepository;
        IBaseRepository<People> peoplerepository;
        ILogger<PagingCommandHandler> logger;
        IMapper mapper;

        public PagingCommandHandler(IBaseRepository<Dangerous> dangerousrepository, IBaseRepository<Car> carrepository, IBaseRepository<People> peoplerepository, ILogger<PagingCommandHandler> logger, IMapper mapper)
        {
            this.dangerousrepository = dangerousrepository;
            this.carrepository = carrepository;
            this.peoplerepository = peoplerepository;
            this.logger = logger;
            this.mapper = mapper;
        }

        public async Task<ApiResult<ApiPaging<List<PagingDangerousRespDto>>>> Handle(PagingCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var result = new ApiResult<ApiPaging<List<PagingDangerousRespDto>>>();

                var dangerous = dangerousrepository.Query(d => d.IsDelete == 0);
                if (!string.IsNullOrEmpty(request.DangerousTitle))
                {
                    dangerous = dangerous.Where(d => d.DangerousTitle.Contains(request.DangerousTitle));
                }
                if (!string.IsNullOrEmpty(Convert.ToString(request.DangerousTime)))
                {
                    dangerous = dangerous.Where(d => d.DangerousTime.Equals(request.DangerousTime));
                }
                var car = carrepository.Query(d => d.CarId > 0);
                if (!string.IsNullOrEmpty(request.CarName))
                {
                    car = car.Where(d => d.CarName.Equals(request.CarName));
                }
                var people = peoplerepository.Query(d => d.PeopleId > 0);
                //if(!string.IsNullOrEmpty(request.PeopleName))
                //{
                //    people = people.Where(d => d.PeopleName.Equals(request.PeopleName));
                //}


                var list = from a in people
                           join b in dangerous
                           on a.PeopleId equals b.PeoPleId
                           join c in car
                           on b.CarId equals c.CarId
                           select b;

                var TotalCount = list.Count();
                var TotalPage = (int)Math.Ceiling(list.Count() * 1.0 / request.PageSize);
                list = list.OrderBy(d => d.DangerousId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);

                var data = mapper.Map<List<PagingDangerousRespDto>>(list);
                foreach (var item in data)
                {
                    var carname = await carrepository.GetAsync(d => d.CarId.Equals(item.CarId));
                    if (carname != null)
                    {
                        item.CarName = carname.CarName;
                    }
                    var peoplename = await peoplerepository.GetAsync(d => d.PeopleId.Equals(item.PeoPleId));
                    if (peoplename != null)
                    {
                        item.PeopleName = peoplename.PeopleName;
                    }
                }

                var page = new ApiPaging<List<PagingDangerousRespDto>>();
                page.TotalCount = TotalCount;
                page.TotalPage = TotalPage;
                page.data = data;
                result.IsSuc = true;

                result.success(page);

                return result;
            }
            catch (Exception ex)
            {
                logger.LogError("事故列表出错" + ex.Message);
                throw;
            }
        }
    }
}
